package com.akuhome.resource.controller;

import com.akuhome.resource.entity.Part;
import com.akuhome.resource.enums.ResultEnum;
import com.akuhome.resource.exception.ResourceException;
import com.akuhome.resource.service.entity.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/part")
public class PartController {

    @Autowired
    PartService partService;
    
    @GetMapping
    public Object findPart(@RequestParam(value="partId",required = false)Integer partId){
        if(partId != null){
            return partService.findPartById(partId);
        }else{
            return partService.findAllPart();
        }
    }
    @GetMapping("/userId/{userId}")
    public Object findPartByUserId(@PathVariable Integer userId){
        if(userId != null) {
            return partService.findPartByUserId(userId);
        }else{
            throw new ResourceException(ResultEnum.ID_EMPTY_ERROR);
        }
    }
    @PostMapping
    public Object insertPart(@Validated @RequestBody Part part, BindingResult bindingResult){
        return partService.insertPart(part);
    }

    @PutMapping
    public Object updatePart(@Validated @RequestBody Part part,BindingResult bindingResult){
        return partService.updatePart(part);
    }

    @DeleteMapping("/{partId}")
    public Object deletePartById(@PathVariable Integer partId){
        return partService.deletePartById(partId);
    }
}
